## VAA effects meta-analysis ----------------------
## moderator analyses -----------------------------

# load packages, data, and functions
source("packages.R")
load("vaa_df_prep.RData")
source("functions.R")


############################################################################
## Moderator models --------------------------------------------------------
############################################################################

## moderator models: study design -------------

vaa_turnout$study_design[vaa_turnout$study_design == "Observational - selection models"] <- "Observational - selection/matching"
vaa_vote$study_design[vaa_vote$study_design == "Observational - selection models"] <- "Observational - selection/matching"


# turnout
tabyl(vaa_turnout$study_design)
turnout_mixeff_study_design <- rma.mv(yi = log_odds, 
                                      V = log_odds_se^2,
                                      mods =  ~ study_design - 1,
                                      random = list(~ 1 | study_label, ~ 1 | election),
                                      data = vaa_turnout,
                                      slab = study_label,
                                      method = "REML")
summary(turnout_mixeff_study_design)

# vote
tabyl(vaa_vote$study_design)
vote_mixeff_study_design <- rma.mv(yi = log_odds, 
                                   V = log_odds_se^2,
                                   mods =  ~ study_design - 1,
                                   random = list(~ 1 | study_label, ~ 1 | election),
                                   data = vaa_vote,
                                   slab = study_label,
                                   method = "REML")
summary(vote_mixeff_study_design)



## moderator models: country -------------

# turnout
tabyl(vaa_turnout$country)
vaa_turnout$country_coarse <- vaa_turnout$country
vaa_turnout$country_coarse[!(vaa_turnout$country_coarse %in% c("Finland", "Germany", "Netherlands", "Switzerland"))] <- "Other"

turnout_mixeff_country <- rma.mv(yi = log_odds, 
                                 V = log_odds_se^2,
                                 mods =  ~ country_coarse - 1,
                                 random = list(~ 1 | study_label, ~ 1 | election),
                                 data = vaa_turnout,
                                 slab = study_label,
                                 method = "REML")
summary(turnout_mixeff_country)

# vote
tabyl(vaa_vote$country)
vaa_vote$country_coarse <- vaa_vote$country
vaa_vote$country_coarse[!(vaa_vote$country_coarse %in% c("Finland", "Germany", "Netherlands", "Switzerland"))] <- "Other"

vote_mixeff_country <- rma.mv(yi = log_odds, 
                              V = log_odds_se^2,
                              mods =  ~ country_coarse - 1,
                              random = list(~ 1 | study_label, ~ 1 | election),
                              data = vaa_vote,
                              slab = study_label,
                              method = "REML")
summary(vote_mixeff_country)


## moderator models: election year -------------


# turnout
tabyl(vaa_turnout$election_year)
vaa_turnout$election_year_coarse <- str_replace(vaa_turnout$election_year, "[:digit:]{1,2}/", "")
turnout_mixeff_year <- rma.mv(yi = log_odds, 
                              V = log_odds_se^2,
                              mods =  ~ as.numeric(election_year_coarse) - 1,
                              random = list(~ 1 | study_label, ~ 1 | election),
                              data = vaa_turnout,
                              slab = study_label,
                              method = "REML")
summary(turnout_mixeff_year)

# vote
tabyl(vaa_vote$election_year)
vaa_vote$election_year_coarse <- str_replace(vaa_vote$election_year, "[:digit:]{1,2}/", "")
vote_mixeff_year <- rma.mv(yi = log_odds, 
                           V = log_odds_se^2,
                           mods =  ~ as.numeric(election_year_coarse) - 1,
                           random = list(~ 1 | study_label, ~ 1 | election),
                           data = vaa_vote,
                           slab = study_label,
                           method = "REML")
summary(vote_mixeff_year)

## moderator models: election year, coarse -------------

#turnout categorization
vaa_turnout$election_year_ord <- vaa_turnout$election_year
vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("1/2015", "9/2015"), "2015", vaa_turnout$election_year_ord)
vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("5/2012", "6/2012"), "2012", vaa_turnout$election_year_ord)


vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("2003", "2004", "2005"), "2003-2005", vaa_turnout$election_year_ord)
vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("2006", "2007", "2008"), "2006-2008", vaa_turnout$election_year_ord)
vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("2009", "2010", "2011"), "2009-2011", vaa_turnout$election_year_ord)
vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("2012", "2013", "2014"), "2012-2014", vaa_turnout$election_year_ord)
vaa_turnout$election_year_ord <- ifelse(vaa_turnout$election_year_ord %in% c("2015", "2016", "2017"), "2015-2017", vaa_turnout$election_year_ord)

#vote categorization
vaa_vote$election_year_ord <- vaa_vote$election_year

vaa_vote$election_year_ord <- ifelse(vaa_vote$election_year_ord %in% c("2003", "2004", "2005"), "2003-2005", vaa_vote$election_year_ord)
vaa_vote$election_year_ord <- ifelse(vaa_vote$election_year_ord %in% c("2006", "2007", "2008"), "2006-2008", vaa_vote$election_year_ord)
vaa_vote$election_year_ord <- ifelse(vaa_vote$election_year_ord %in% c("2009", "2010", "2011"), "2009-2011", vaa_vote$election_year_ord)
vaa_vote$election_year_ord <- ifelse(vaa_vote$election_year_ord %in% c("2012", "2013", "2014"), "2012-2014", vaa_vote$election_year_ord)
vaa_vote$election_year_ord <- ifelse(vaa_vote$election_year_ord %in% c("2015", "2016", "2017"), "2015-2017", vaa_vote$election_year_ord)

# turnout
turnout_mixeff_election_year <- rma.mv(yi = log_odds, 
                                       V = log_odds_se^2,
                                       mods =  ~ election_year_ord - 1,
                                       random = list(~ 1 | study_label, ~ 1 | election),
                                       data = vaa_turnout,
                                       slab = study_label,
                                       method = "REML")
summary(turnout_mixeff_election_year)


# vote
vote_mixeff_election_year <- rma.mv(yi = log_odds, 
                                    V = log_odds_se^2,
                                    mods =  ~ election_year_ord - 1,
                                    random = list(~ 1 | study_label, ~ 1 | election),
                                    data = vaa_vote,
                                    slab = study_label,
                                    method = "REML")
summary(vote_mixeff_election_year)


## moderator models: sample size -------------

# turnout
turnout_mixeff_sample <- rma.mv(yi = log_odds, 
                                V = log_odds_se^2,
                                mods =  ~ sample_size_ord - 1,
                                random = list(~ 1 | study_label, ~ 1 | election),
                                data = vaa_turnout,
                                slab = study_label,
                                method = "REML")
summary(turnout_mixeff_sample)


# vote
vote_mixeff_sample <- rma.mv(yi = log_odds, 
                             V = log_odds_se^2,
                             mods =  ~ sample_size_ord - 1,
                             random = list(~ 1 | study_label, ~ 1 | election),
                             data = vaa_vote,
                             slab = study_label,
                             method = "REML")
summary(vote_mixeff_sample)


## moderator models: election type -------------

vaa_turnout$election_type_cat <- ifelse(vaa_turnout$election_type == "parliamentary", "First order", "Second order")
vaa_vote$election_type_cat <- ifelse(vaa_vote$election_type == "parliamentary", "First order", "Second order")

# turnout
turnout_mixeff_election_type <- rma.mv(yi = log_odds, 
                                       V = log_odds_se^2,
                                       mods =  ~ election_type_cat - 1,
                                       random = list(~ 1 | study_label, ~ 1 | election),
                                       data = vaa_turnout,
                                       slab = study_label,
                                       method = "REML")
summary(turnout_mixeff_election_type)


# vote
vote_mixeff_election_type <- rma.mv(yi = log_odds, 
                                    V = log_odds_se^2,
                                    mods =  ~ election_type_cat - 1,
                                    random = list(~ 1 | study_label, ~ 1 | election),
                                    data = vaa_vote,
                                    slab = study_label,
                                    method = "REML")
summary(vote_mixeff_election_type)


## moderator models: vote change operationalization -------------

vaa_vote$depvar_cat <- ifelse(vaa_vote$depvar == "vote switching (from previous election)", "Between elections", "In-campaign")

# vote
vote_mixeff_vote_change <- rma.mv(yi = log_odds, 
                                  V = log_odds_se^2,
                                  mods =  ~ depvar_cat - 1,
                                  random = list(~ 1 | study_label, ~ 1 | election),
                                  data = vaa_vote,
                                  slab = study_label,
                                  method = "REML")
summary(vote_mixeff_vote_change)


## moderator models: sample type -------------

# turnout
turnout_mixeff_sampling_type <- rma.mv(yi = log_odds, 
                                       V = log_odds_se^2,
                                       mods =  ~ sampling_type - 1,
                                       random = list(~ 1 | study_label, ~ 1 | election),
                                       data = vaa_turnout,
                                       slab = study_label,
                                       method = "REML")
summary(turnout_mixeff_sampling_type)


# vote
vote_mixeff_sampling_type <- rma.mv(yi = log_odds, 
                                    V = log_odds_se^2,
                                    mods =  ~ sampling_type - 1,
                                    random = list(~ 1 | study_label, ~ 1 | election),
                                    data = vaa_vote,
                                    slab = study_label,
                                    method = "REML")
summary(vote_mixeff_sampling_type)



## combined models ------------------

# turnout
turnout_mixeff_combined <- rma.mv(yi = log_odds, 
                                  V = log_odds_se^2,
                                  mods =  ~ study_design + election_year_ord + sample_size_ord + election_type_cat + sampling_type,
                                  random = list(~ 1 | study_label, ~ 1 | election),
                                  data = vaa_turnout,
                                  slab = study_label,
                                  method = "REML")
summary(turnout_mixeff_combined, digits = 2)

# vote
vote_mixeff_combined <- rma.mv(yi = log_odds, 
                               V = log_odds_se^2,
                               mods =  ~ study_design + election_year_ord + sample_size_ord + election_type_cat + sampling_type,
                               random = list(~ 1 | study_label, ~ 1 | election),
                               data = vaa_vote,
                               slab = study_label,
                               method = "REML")
summary(vote_mixeff_combined, digits = 2)


############################################################################
## Moderator model plots ---------------------------------------------------
############################################################################

## Figure 6. Estimated effects of study design on turnout and vote choice
# moderator levels 
mod_lvls <- c("Experimental", "Observational - selection/matching", "Observational - panel", "Observational - no panel")

# turnout models
turnout_meta_df <- data.frame(depvar = "turnout",
                              moderator = sort(unique(vaa_turnout$study_design)),
                              k = as.numeric(table(vaa_turnout$study_design)),
                              stringsAsFactors = FALSE)

pred_mat <- 
  rbind(
    c(TRUE, FALSE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE, FALSE),
    c(FALSE, FALSE, TRUE, FALSE),
    c(FALSE, FALSE, FALSE, TRUE)
  )
colnames(pred_mat) <- names(coef(turnout_mixeff_study_design))

pred_df <- predict(turnout_mixeff_study_design, 
                   newmods = pred_mat) %>% as.data.frame() %>% exp()

turnout_studydesign_pred_df <- cbind(turnout_meta_df, pred_df)
turnout_studydesign_pred_df <- arrange(turnout_studydesign_pred_df, factor(moderator, levels = mod_lvls))

# vote choice models
vote_meta_df <- data.frame(depvar = "vote",
                           moderator = sort(unique(vaa_vote$study_design)),
                           k = as.numeric(table(vaa_vote$study_design)),
                           stringsAsFactors = FALSE)

pred_mat <- 
  rbind(
    c(TRUE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE),
    c(FALSE, FALSE, TRUE)
  )
colnames(pred_mat) <- names(coef(vote_mixeff_study_design))

pred_df <- predict(vote_mixeff_study_design, 
                   newmods = pred_mat) %>% as.data.frame() %>% exp()

vote_studydesign_pred_df <- cbind(vote_meta_df, pred_df)
vote_studydesign_pred_df <- arrange(vote_studydesign_pred_df, factor(moderator, levels = mod_lvls))


## create plot
pdf(file="effects-studydesign.pdf", height=3, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,13,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1.9, 1), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 4.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:4, labels = F, tick = F)
text(y = 1:4, par("usr")[1], labels = turnout_studydesign_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:4 + .03, .4, labels = paste0('k = ', turnout_studydesign_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# Experimental
y_par <- 1
dat <- filter(turnout_studydesign_pred_df, moderator == "Experimental")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Observational - selection/matching
y_par <- 2
dat <- filter(turnout_studydesign_pred_df, moderator == "Observational - selection/matching")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Observational - panel
y_par <- 3
dat <- filter(turnout_studydesign_pred_df, moderator == "Observational - panel")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Observational - no panel
y_par <- 4
dat <- filter(turnout_studydesign_pred_df, moderator == "Observational - no panel")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 4.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:4, labels = F, tick = F)
text(y = 1:4, par("usr")[1], labels = vote_studydesign_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:3 + .03, .4, labels = paste0('k = ', vote_studydesign_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
text(y = 4 + .03, .4, labels = paste0('k = ', 0), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# Experimental
y_par <- 1
dat <- filter(vote_studydesign_pred_df, moderator == "Experimental")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Observational - selection/matching
y_par <- 2
dat <- filter(vote_studydesign_pred_df, moderator == "Observational - selection/matching")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Observational - panel
y_par <- 3
dat <- filter(vote_studydesign_pred_df, moderator == "Observational - panel")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .65)
dev.off()


## Figure B1. coefficient plot: country -----------------------------

# moderator levels 
mod_lvls <- c("Other", "Switzerland", "Netherlands", "Germany", "Finland")

# turnout models
turnout_meta_df <- data.frame(depvar = "turnout",
                              moderator = sort(unique(vaa_turnout$country_coarse)),
                              k = as.numeric(table(vaa_turnout$country_coarse)),
                              stringsAsFactors = FALSE)

pred_mat <- 
  rbind(
    c(TRUE, FALSE, FALSE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE, FALSE, FALSE),
    c(FALSE, FALSE, TRUE, FALSE, FALSE),
    c(FALSE, FALSE, FALSE, TRUE, FALSE),
    c(FALSE, FALSE, FALSE, FALSE, TRUE)
  )
colnames(pred_mat) <- names(coef(turnout_mixeff_country))

pred_df <- predict(turnout_mixeff_country, 
                   newmods = pred_mat) %>% as.data.frame() %>% exp()

turnout_country_pred_df <- cbind(turnout_meta_df, pred_df)
turnout_country_pred_df <- arrange(turnout_country_pred_df, factor(moderator, levels = mod_lvls))

# vote choice models
vote_meta_df <- data.frame(depvar = "vote",
                           moderator = sort(unique(vaa_vote$country_coarse)),
                           k = as.numeric(table(vaa_vote$country_coarse)),
                           stringsAsFactors = FALSE)

pred_mat <- 
  rbind(
    c(TRUE, FALSE, FALSE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE, FALSE, FALSE),
    c(FALSE, FALSE, TRUE, FALSE, FALSE),
    c(FALSE, FALSE, FALSE, TRUE, FALSE),
    c(FALSE, FALSE, FALSE, FALSE, TRUE)
  )
colnames(pred_mat) <- names(coef(vote_mixeff_country))

pred_df <- predict(vote_mixeff_country, 
                   newmods = pred_mat) %>% as.data.frame() %>% exp()

vote_country_pred_df <- cbind(vote_meta_df, pred_df)
vote_country_pred_df <- arrange(vote_country_pred_df, factor(moderator, levels = mod_lvls))


## create plot
pdf(file="effects-country.pdf", height=3.5, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,7,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1.4, 1), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 5.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:4, labels = F, tick = F)
text(y = 1:5, par("usr")[1], labels = turnout_country_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:5 + .03, .4, labels = paste0('k = ', turnout_country_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5, 4.5), lty = 1, col = "darkgrey")
# Other
y_par <- 1
dat <- filter(turnout_country_pred_df, moderator == "Other")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Switzerland
y_par <- 2
dat <- filter(turnout_country_pred_df, moderator == "Switzerland")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Netherlands
y_par <- 3
dat <- filter(turnout_country_pred_df, moderator == "Netherlands")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Germany
y_par <- 4
dat <- filter(turnout_country_pred_df, moderator == "Germany")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Finland
y_par <- 5
dat <- filter(turnout_country_pred_df, moderator == "Finland")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 5.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:5, labels = F, tick = F)
text(y = 1:5, par("usr")[1], labels = vote_country_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:5 + .03, .4, labels = paste0('k = ', vote_country_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5, 4.5), lty = 1, col = "darkgrey")
# Other
y_par <- 1
dat <- filter(vote_country_pred_df, moderator == "Other")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Switzerland
y_par <- 2
dat <- filter(vote_country_pred_df, moderator == "Switzerland")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Netherlands
y_par <- 3
dat <- filter(vote_country_pred_df, moderator == "Netherlands")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Germany
y_par <- 4
dat <- filter(vote_country_pred_df, moderator == "Germany")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# Finland
y_par <- 5
dat <- filter(vote_country_pred_df, moderator == "Finland")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .58)
dev.off()


## Figure B2. coefficient plot: election year -----------------------------


# moderator levels 
mod_lvls_election <- c("2015-2017", "2012-2014", "2009-2011", "2006-2008", "2003-2005")

# turnout models
turnout_meta_df <- data.frame(depvar = "turnout",
                              moderator = sort(unique(vaa_turnout$election_year_ord)),
                              k = as.numeric(table(vaa_turnout$election_year_ord)),
                              stringsAsFactors = FALSE)

pred_mat <- 
  rbind(
    c(TRUE, FALSE, FALSE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE, FALSE, FALSE),
    c(FALSE, FALSE, TRUE, FALSE, FALSE),
    c(FALSE, FALSE, FALSE, TRUE, FALSE),
    c(FALSE, FALSE, FALSE, FALSE, TRUE)
  )
colnames(pred_mat) <- names(coef(turnout_mixeff_election_year))

pred_df <- predict(turnout_mixeff_election_year, 
                   newmods = pred_mat) %>% as.data.frame() %>% exp()

turnout_election_year_pred_df <- cbind(turnout_meta_df, pred_df)
turnout_election_year_pred_df <- arrange(turnout_election_year_pred_df, factor(moderator, levels = mod_lvls_election))

# vote choice models
vote_meta_df <- data.frame(depvar = "vote",
                           moderator = sort(unique(vaa_vote$election_year_ord)),
                           k = as.numeric(table(vaa_vote$election_year_ord)),
                           stringsAsFactors = FALSE)

pred_mat <- 
  rbind(
    c(TRUE, FALSE, FALSE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE, FALSE, FALSE),
    c(FALSE, FALSE, TRUE, FALSE, FALSE),
    c(FALSE, FALSE, FALSE, TRUE, FALSE),
    c(FALSE, FALSE, FALSE, FALSE, TRUE)
  )
colnames(pred_mat) <- names(coef(vote_mixeff_election_year))

pred_df <- predict(vote_mixeff_election_year, 
                   newmods = pred_mat) %>% as.data.frame() %>% exp()

vote_election_year_pred_df <- cbind(vote_meta_df, pred_df)
vote_election_year_pred_df <- arrange(vote_election_year_pred_df, factor(moderator, levels = mod_lvls_election))


## create plot
pdf(file="effects-election-year.pdf", height=3.5, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,7,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1.4, 1), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 5.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:4, labels = F, tick = F)
text(y = 1:5, par("usr")[1], labels = turnout_election_year_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:5 + .03, .4, labels = paste0('k = ', turnout_election_year_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5, 4.5), lty = 1, col = "darkgrey")
# 2015-2017
y_par <- 1
dat <- filter(turnout_election_year_pred_df, moderator == "2015-2017")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2012-2014
y_par <- 2
dat <- filter(turnout_election_year_pred_df, moderator == "2012-2014")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2009-2011
y_par <- 3
dat <- filter(turnout_election_year_pred_df, moderator == "2009-2011")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2006-2008
y_par <- 4
dat <- filter(turnout_election_year_pred_df, moderator == "2006-2008")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2003-2005
y_par <- 5
dat <- filter(turnout_election_year_pred_df, moderator == "2003-2005")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 5.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:5, labels = F, tick = F)
text(y = 1:5, par("usr")[1], labels = vote_election_year_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:5 + .03, .4, labels = paste0('k = ', vote_election_year_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5, 4.5), lty = 1, col = "darkgrey")
# 2015-2017
y_par <- 1
dat <- filter(vote_election_year_pred_df, moderator == "2015-2017")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2012-2014
y_par <- 2
dat <- filter(vote_election_year_pred_df, moderator == "2012-2014")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2009-2011
y_par <- 3
dat <- filter(vote_election_year_pred_df, moderator == "2009-2011")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2006-2008
y_par <- 4
dat <- filter(vote_election_year_pred_df, moderator == "2006-2008")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# 2003-2005
y_par <- 5
dat <- filter(vote_election_year_pred_df, moderator == "2003-2005")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .58)
dev.off()


## Figure B3. coefficient plot: election type ----------------

# moderator levels 
mod_lvls_election_type <- c("Second order","First order")

# turnout models
turnout_meta_df_election_type <- data.frame(depvar = "turnout",
                                            moderator = sort(unique(vaa_turnout$election_type_cat)),
                                            k = as.numeric(table(vaa_turnout$election_type_cat)),
                                            stringsAsFactors = FALSE)

pred_mat_election_type <- 
  rbind(
    c(TRUE, FALSE),
    c(FALSE, TRUE))

colnames(pred_mat_election_type) <- names(coef(turnout_mixeff_election_type))

pred_df_election_type <- predict(turnout_mixeff_election_type, 
                                 newmods = pred_mat_election_type) %>% as.data.frame() %>% exp()

turnout_election_type_pred_df <- cbind(turnout_meta_df_election_type, pred_df_election_type)
turnout_election_type_pred_df <- arrange(turnout_election_type_pred_df, factor(moderator, levels = mod_lvls_election_type))

# vote choice models
vote_meta_df_election_type<- data.frame(depvar = "vote",
                                        moderator = sort(unique(vaa_vote$election_type_cat)),
                                        k = as.numeric(table(vaa_vote$election_type_cat)),
                                        stringsAsFactors = FALSE)

pred_mat_election_type <- 
  rbind(
    c(TRUE, FALSE),
    c(FALSE, TRUE)
  )
colnames(pred_mat_election_type) <- names(coef(vote_mixeff_election_type))

pred_df_election_type <- predict(vote_mixeff_election_type, 
                                 newmods = pred_mat_election_type) %>% as.data.frame() %>% exp()

vote_election_type_pred_df <- cbind(vote_meta_df_election_type, pred_df_election_type)
vote_election_type_pred_df <- arrange(vote_election_type_pred_df, factor(moderator, levels = mod_lvls_election_type))


## create plot
pdf(file="effects-election-type.pdf", height=2.3, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,6,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1, .7), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 2.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:2, labels = F, tick = F)
text(y = 1:2, par("usr")[1], labels = turnout_election_type_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:2 + .03, .4, labels = paste0('k = ', turnout_election_type_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# second order
y_par <- 1
dat <- filter(turnout_election_type_pred_df, moderator == "Second order")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# first order
y_par <- 2
dat <- filter(turnout_election_type_pred_df, moderator == "First order")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])


## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 2.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:2, labels = F, tick = F)
text(y = 1:2, par("usr")[1], labels = vote_election_type_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:2 + .03, .4, labels = paste0('k = ', vote_election_type_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
#second order
y_par <- 1
dat <- filter(vote_election_type_pred_df, moderator == "Second order")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# first order
y_par <- 2
dat <- filter(vote_election_type_pred_df, moderator == "First order")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .58)
dev.off()


## Figure B4. coefficient plot: sample size ----------------

# moderator levels 

mod_lvls_sample <- c("Large (>3000)", "Medium (2000-3000)", "Small (<1000)")

# turnout models
turnout_meta_df_sample <- data.frame(depvar = "turnout",
                                     moderator = sort(unique(vaa_turnout$sample_size_ord)),
                                     k = as.numeric(table(vaa_turnout$sample_size_ord)),
                                     stringsAsFactors = FALSE)

pred_mat_sample <- 
  rbind(
    c(TRUE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE),
    c(FALSE, FALSE, TRUE))

colnames(pred_mat_sample) <- names(coef(turnout_mixeff_sample))

pred_df_sample <- predict(turnout_mixeff_sample, 
                          newmods = pred_mat_sample) %>% as.data.frame() %>% exp()

turnout_sample_pred_df <- cbind(turnout_meta_df_sample, pred_df_sample)
turnout_sample_pred_df <- arrange(turnout_sample_pred_df, factor(moderator, levels = mod_lvls_sample))

# vote choice models
vote_meta_df_sample <- data.frame(depvar = "vote",
                                  moderator = sort(unique(vaa_vote$sample_size_ord)),
                                  k = as.numeric(table(vaa_vote$sample_size_ord)),
                                  stringsAsFactors = FALSE)

pred_mat_sample <- 
  rbind(
    c(TRUE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE),
    c(FALSE, FALSE, TRUE)
  )
colnames(pred_mat_sample) <- names(coef(vote_mixeff_sample))

pred_df_sample <- predict(vote_mixeff_sample, 
                          newmods = pred_mat_sample) %>% as.data.frame() %>% exp()

vote_sample_pred_df <- cbind(vote_meta_df_sample, pred_df_sample)
vote_sample_pred_df <- arrange(vote_sample_pred_df, factor(moderator, levels = mod_lvls_sample))


## create plot
pdf(file="effects-samplesize.pdf", height=2.3, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,8.3,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1.5, 1), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 3.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:3, labels = F, tick = F)
text(y = 1:3, par("usr")[1], labels = turnout_sample_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:3 + .03, .4, labels = paste0('k = ', turnout_sample_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# large
y_par <- 1
dat <- filter(turnout_sample_pred_df, moderator == "Large (>3000)")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# medium
y_par <- 2
dat <- filter(turnout_sample_pred_df, moderator == "Medium (2000-3000)")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# small
y_par <- 3
dat <- filter(turnout_sample_pred_df, moderator == "Small (<1000)")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])


## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 3.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:3, labels = F, tick = F)
text(y = 1:3, par("usr")[1], labels = vote_sample_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:3 + .03, .4, labels = paste0('k = ', vote_sample_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
text(y = 4 + .03, .4, labels = paste0('k = ', 0), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# large
y_par <- 1
dat <- filter(vote_sample_pred_df, moderator == "Large (>3000)")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# medium
y_par <- 2
dat <- filter(vote_sample_pred_df, moderator == "Medium (2000-3000)")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# small
y_par <- 3
dat <- filter(vote_sample_pred_df, moderator == "Small (<1000)")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .58)
dev.off()


## Figure B5. coefficient plot: sample type ----------------

# moderator levels 
mod_lvls_sampling_type <- c("Probability","Non-probability", "Convenience")

# turnout models
turnout_meta_df_sampling_type <- data.frame(depvar = "turnout",
                                            moderator = sort(unique(vaa_turnout$sampling_type)),
                                            k = as.numeric(table(vaa_turnout$sampling_type)),
                                            stringsAsFactors = FALSE)

pred_mat_sampling_type <- 
  rbind(
    c(TRUE, FALSE),
    c(FALSE, TRUE))

colnames(pred_mat_sampling_type) <- names(coef(turnout_mixeff_sampling_type))

pred_df_sampling_type <- predict(turnout_mixeff_sampling_type, 
                                 newmods = pred_mat_sampling_type) %>% as.data.frame() %>% exp()

turnout_sampling_type_pred_df <- cbind(turnout_meta_df_sampling_type, pred_df_sampling_type)
turnout_sampling_type_pred_df <- arrange(turnout_sampling_type_pred_df, factor(moderator, levels = mod_lvls_sampling_type))

# vote choice models
vote_meta_df_sampling_type <- data.frame(depvar = "vote",
                                         moderator = sort(unique(vaa_vote$sampling_type)),
                                         k = as.numeric(table(vaa_vote$sampling_type)),
                                         stringsAsFactors = FALSE)

pred_mat_sampling_type <- 
  rbind(
    c(TRUE, FALSE, FALSE),
    c(FALSE, TRUE, FALSE),
    c(FALSE, FALSE, TRUE)
  )
colnames(pred_mat_sampling_type) <- names(coef(vote_mixeff_sampling_type))

pred_df_sampling_type <- predict(vote_mixeff_sampling_type, 
                                 newmods = pred_mat_sampling_type) %>% as.data.frame() %>% exp()

vote_sampling_type_pred_df <- cbind(vote_meta_df_sampling_type, pred_df_sampling_type)
vote_sampling_type_pred_df <- arrange(vote_sampling_type_pred_df, factor(moderator, levels = mod_lvls_sampling_type))


## create plot
pdf(file="effects-sampletype.pdf", height=2.3, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,7,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1, .7), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 3.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:3, labels = F, tick = F)
text(y = 1:2, par("usr")[1], labels = turnout_sampling_type_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 3, par("usr")[1], labels = paste0('Convenience'), srt = 0, pos = 2, xpd = TRUE)
text(y = 1:2 + .03, .3, labels = paste0('k = ', turnout_sampling_type_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
text(y = 3 + .03, .3, labels = paste0('k = ', 0), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# prob
y_par <- 1
dat <- filter(turnout_sampling_type_pred_df, moderator == "Probability")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# non-prob
y_par <- 2
dat <- filter(turnout_sampling_type_pred_df, moderator == "Non-probability")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])


## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(-0.2, 4), ylim = c(.7, 3.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:3, labels = F, tick = F)
text(y = 1:3, par("usr")[1], labels = vote_sampling_type_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:3 + .03, .1, labels = paste0('k = ', vote_sampling_type_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
#text(y = 4 + .03, .1, labels = paste0('k = ', 0), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
#prob
y_par <- 1
dat <- filter(vote_sampling_type_pred_df, moderator == "Probability")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# non-prob
y_par <- 2
dat <- filter(vote_sampling_type_pred_df, moderator == "Non-probability")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# convenience
y_par <- 3
dat <- filter(vote_sampling_type_pred_df, moderator == "Convenience")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .58)
dev.off()



## Figure B6. coefficient plot: vote choice operationalization ----------------

# moderator levels 

mod_lvls_vote_change <- c("Between elections", "In-campaign")


# vote choice models
vote_meta_df_vote_change <- data.frame(depvar = "vote",
                                       moderator = sort(unique(vaa_vote$depvar_cat)),
                                       k = as.numeric(table(vaa_vote$depvar_cat)),
                                       stringsAsFactors = FALSE)

pred_mat_vote_change <- 
  rbind(
    c(TRUE, FALSE),
    c(FALSE, TRUE)
  )
colnames(pred_mat_vote_change) <- names(coef(vote_mixeff_vote_change))

pred_df_vote_change <- predict(vote_mixeff_vote_change, 
                               newmods = pred_mat_vote_change) %>% as.data.frame() %>% exp()

vote_change_pred_df <- cbind(vote_meta_df_vote_change, pred_df_vote_change)
vote_change_pred_df <- arrange(vote_change_pred_df, factor(moderator, levels = mod_lvls_sample))


## create plot
pdf(file="effects-votechoice.pdf", height=2.3, width=5, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,7.5,2,1))
layout(matrix(1, 1, byrow = TRUE), widths=c(1), heights=c(1))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(0, 4), ylim = c(.7, 2.3), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:2, labels = F, tick = F)
text(y = 1:2, par("usr")[1], labels = vote_change_pred_df$moderator, srt = 0, pos = 2, xpd = TRUE)
text(y = 1:2 + .03, .4, labels = paste0('k = ', vote_change_pred_df$k), font = 3, srt = 0, cex = .8, xpd = TRUE)
axis(1, at = seq(0, 4, 1), labels = seq(0, 4, 1), tick = T)
mtext("Vote Choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(.5, 1.5, 2.5, 3.5), lty = 1, col = "darkgrey")
# between elections
y_par <- 1
dat <- filter(vote_change_pred_df, moderator == "Between elections")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])
# in-campaign
y_par <- 2
dat <- filter(vote_change_pred_df, moderator == "In-campaign")

arrows(x0 = dat$ci.lb, y0 = y_par, x1 = dat$ci.ub, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$pred, y_par, pch = 20, cex = 2, col = colors[1])

mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .63)
dev.off()


## Figure B7. coefficient plot: combined model ----------------


## coefficient plot: combined
# moderator levels 
mod_lvls <- c("Observational - no panel", "Observational - panel", "Observational - selection/matching", "2006-2008", "2009-2011", "2012-2014", "2015-2017", "Medium (2000-3000)", "Small (<1000)", "Second order", "Probability sample")
#turnout
k <- c(15, 3, 7, 6, 9, 7, 4, 14, 8, 5, 16)
turnout_pred_df <- table.rma(turnout_mixeff_combined) %>% as.data.frame() %>% .[-c(3,6)] %>% exp() %>% .[-c(1),] %>% mutate(moderator = mod_lvls, k = k)

#vote
mod <- c("Observational - panel", "Observational - selection/matching", "2006-2008", "2009-2011", "2012-2014", "2015-2017", "Medium (2000-3000)", "Small (<1000)", "Second order", "Non-probability sample", "Probability sample")
k <- c(14, 2, 5, 10, 2, 2, 12, 6, 3, 9, 10)
vote_pred_df <- table.rma(vote_mixeff_combined) %>% as.data.frame() %>% .[-c(3,6)] %>% exp() %>% .[-c(1),] %>% mutate(moderator = mod, k = k)


## plot
pdf(file="effects-combined.pdf", height=4.5, width=8, family="Times")
par(oma=c(1,0,.5,0))
par(mar=c(3,13,2,1))
layout(matrix(1:2, 1, byrow = TRUE), widths=c(1.95, 1), heights=c(2, 2))
colors <- c("black", "darkgrey")
## turnout panel
plot(0, 0, xlim = c(-1, 6), ylim = c(1, 12), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:6, labels = F, tick = F)
text(y = 1:12, par("usr")[1], labels = c(turnout_pred_df$moderator, "Non-probability sample"), srt = 0, pos = 2, xpd = TRUE)
text(y = 1:11 + .01, -.5, labels = paste0('k = ', turnout_pred_df$k), font = 3, srt = 0, cex = .7, xpd = TRUE)
text(y = 12 + .01, -.5, labels = paste0('k = ', 0), font = 3, srt = 0, cex = .7, xpd = TRUE)
axis(1, at = seq(0, 6, 1), labels = seq(0, 6, 1), tick = T)
mtext("Turnout", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5), lty = 1, col = "darkgrey")
# Obs - no panel
y_par <- 1
dat <- filter(turnout_pred_df, moderator == "Observational - no panel")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Obs - panel
y_par <- 2
dat <- filter(turnout_pred_df, moderator == "Observational - panel")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Obs -selmod
y_par <- 3
dat <- filter(turnout_pred_df, moderator == "Observational - selection/matching")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2006.2008
y_par <- 4
dat <- filter(turnout_pred_df, moderator == "2006-2008")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2009-2011
y_par <- 5
dat <- filter(turnout_pred_df, moderator == "2009-2011")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2012-2014
y_par <- 6
dat <- filter(turnout_pred_df, moderator == "2012-2014")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2015-2017
y_par <- 7
dat <- filter(turnout_pred_df, moderator == "2015-2017")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Medium (2000-3000)
y_par <- 8
dat <- filter(turnout_pred_df, moderator == "Medium (2000-3000)")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Small(<1000)
y_par <- 9
dat <- filter(turnout_pred_df, moderator == "Small (<1000)")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Second order
y_par <- 10
dat <- filter(turnout_pred_df, moderator == "Second order")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# probability sample
y_par <- 11
dat <- filter(turnout_pred_df, moderator == "Probability sample")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])

## vote choice panel
par(mar=c(3,0,2,1))
plot(0, 0, xlim = c(-1, 6), ylim = c(1, 12), pch = 20, col = "white", xlab = "", ylab = "", yaxt = "n", xaxt = "n")
axis(2, at = 0:6, labels = F, tick = F)
text(y = 2:12 + .01, -.5, labels = paste0('k = ', vote_pred_df$k), font = 3, srt = 0, cex = .7, xpd = TRUE)
text(y = 1 + .01, -.5, labels = paste0('k = ', 0), font = 3, srt = 0, cex = .7, xpd = TRUE)
axis(1, at = seq(0, 6, 1), labels = seq(0, 6, 1), tick = T)
mtext("Vote choice", side = 3, line = .7, outer = FALSE)
abline(v = 1, lty = 2)
abline(h = c(1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5), lty = 1, col = "darkgrey")
# Obs - panel
y_par <- 2
dat <- filter(vote_pred_df, moderator == "Observational - panel")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Obs - selmod
y_par <- 3
dat <- filter(vote_pred_df, moderator == "Observational - selection/matching")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2006-2008
y_par <- 4
dat <- filter(vote_pred_df, moderator == "2006-2008")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2009-2011
y_par <- 5
dat <- filter(vote_pred_df, moderator == "2009-2011")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2012-2014
y_par <- 6
dat <- filter(vote_pred_df, moderator == "2012-2014")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# 2015-2017
y_par <- 7
dat <- filter(vote_pred_df, moderator == "2015-2017")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Medium (2000-3000)
y_par <- 8
dat <- filter(vote_pred_df, moderator == "Medium (2000-3000)")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Small (<1000)
y_par <- 9
dat <- filter(vote_pred_df, moderator == "Small (<1000)")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# Second order
y_par <- 10
dat <- filter(vote_pred_df, moderator == "Second order")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# non-probability sample
y_par <- 11
dat <- filter(vote_pred_df, moderator == "Non-probability sample")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])
# probability sample
y_par <- 12
dat <- filter(vote_pred_df, moderator == "Probability sample")

arrows(x0 = dat$conf.low, y0 = y_par, x1 = dat$conf.high, y1 = y_par, length = .02, angle = 90, code = 3, lwd = 1, col = colors[1])
points(dat$estimate, y_par, pch = 20, cex = 2, col = colors[1])

# joint x axis label
mtext("Estimated odds ratio", side = 1, line = -.5, outer = TRUE, at = .64)
dev.off()


############################################################################
## Moderator model output tables -------------------------------------------
############################################################################


### create regression tables ---------------------------------------------------------

mixeff_cols <- c("Estimate", "k" ,"Std. Error", "Z. Value", "Conf. Low", "Conf. High", "P. Value")

## Tables B1 and B2. study design -------------------------
#turnout
turnout_study_design_tab <- table.rma(turnout_mixeff_study_design)
rownames(turnout_study_design_tab) <- gsub("study_design", "", rownames(turnout_study_design_tab))
turnout_study_design_tab <- round(turnout_study_design_tab,2)
turnout_study_design_tab$estimate <- ifelse(between(turnout_study_design_tab$p.value, 0, 0.001), paste0(turnout_study_design_tab$estimate,"^{***}"), 
                                            ifelse(between(turnout_study_design_tab$p.value, 0.001, 0.01), paste0(turnout_study_design_tab$estimate,"^{**}"), 
                                                   ifelse(between(turnout_study_design_tab$p.value, 0.01, 0.05), paste0(turnout_study_design_tab$estimate,"^{*}"), turnout_study_design_tab$estimate)))
turnout_study_design_tab$p.value <- ifelse(turnout_study_design_tab$p.value < 0.001, "<0.001", turnout_study_design_tab$p.value)

turnout_study_design_tab$k <- as.numeric(table(vaa_turnout$study_design))

turnout_study_design_latex<- latex_reg(turnout_study_design_tab)

colnames(turnout_study_design_latex) <- mixeff_cols
rownames(turnout_study_design_latex) <- rownames(turnout_study_design_tab)

com_turnout_study_design  <- paste0("\\bottomrule\n \\multicolumn{8}{p{15.5cm}}",
                                    "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 26) = 133.0338, p-val < .0001, QM(df = 4) = 44.9021, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_turnout_study_design)-1)

print(xtable(turnout_study_design_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Turnout study design results of mixed effects moderator analysis \\label{tab:turnoutstudydesign}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(4), command = com_turnout_study_design ), sanitize.text.function = function(x) {x}, 
      file = "turnout-study-design-latex.tex")

#vote
vote_study_design_tab <- table.rma(vote_mixeff_study_design)
rownames(vote_study_design_tab) <- gsub("study_design", "", rownames(vote_study_design_tab))
vote_study_design_tab <- round(vote_study_design_tab,2)
vote_study_design_tab$estimate <- ifelse(between(vote_study_design_tab$p.value, 0, 0.001), paste0(vote_study_design_tab$estimate,"^{***}"), 
                                         ifelse(between(vote_study_design_tab$p.value, 0.001, 0.01), paste0(vote_study_design_tab$estimate,"^{**}"), 
                                                ifelse(between(vote_study_design_tab$p.value, 0.01, 0.05), paste0(vote_study_design_tab$estimate,"^{*}"),  paste0(vote_study_design_tab$estimate))))
vote_study_design_tab$p.value <- ifelse(vote_study_design_tab$p.value < 0.001, "<0.001", vote_study_design_tab$p.value)
vote_study_design_tab$k <- as.numeric(table(vaa_vote$study_design))

vote_study_design_latex<- latex_reg(vote_study_design_tab)

colnames(vote_study_design_latex) <- mixeff_cols
rownames(vote_study_design_latex) <- rownames(vote_study_design_tab)

com_vote_study_design  <-  paste0("\\bottomrule\n \\multicolumn{8}{p{15.5cm}}",
                                  "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The test for residual heterogeneity is not significant at the 95\\%: QE(df = 18) = 15.0816, p-val = 0.6564. The test of moderators is significant:  QM(df = 3) = 210.7735, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_study_design)-1)

print(xtable(vote_study_design_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Vote choice study design results of mixed effects moderator analysis \\label{tab:votestudydesign}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(3), command = com_vote_study_design ), sanitize.text.function = function(x) {x}, 
      file = "vote-study-design-latex.tex")

## Tables B3 and B4. country effects --------------------
#turnout
turnout_country_tab <- table.rma(turnout_mixeff_country)
rownames(turnout_country_tab) <- gsub("country_coarse", "", rownames(turnout_country_tab))
turnout_country_tab <- round(turnout_country_tab,2)
turnout_country_tab$estimate <- ifelse(between(turnout_country_tab$p.value, 0, 0.001), paste0(turnout_country_tab$estimate,"^{***}"), 
                                       ifelse(between(turnout_country_tab$p.value, 0.001, 0.01), paste0(turnout_country_tab$estimate,"^{**}"), 
                                              ifelse(between(turnout_country_tab$p.value, 0.01, 0.05), paste0(turnout_country_tab$estimate,"^{*}"), turnout_country_tab$estimate)))
turnout_country_tab$p.value <- ifelse(turnout_country_tab$p.value < 0.001, "<0.001", turnout_country_tab$p.value)
turnout_country_tab$k <- as.numeric(table(vaa_turnout$country_coarse))

turnout_country_latex<- latex_reg(turnout_country_tab)

colnames(turnout_country_latex) <- mixeff_cols
rownames(turnout_country_latex) <- rownames(turnout_country_tab)

com_turnout_country  <- paste0("\\bottomrule \n \\multicolumn{8}{p{13cm}}",
                               "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 25) = 102.3942, p-val < .0001, QM(df = 5) = 58.2662, p-val < .0001 }} \\\\\n")

hlines <- c(-1, 0, nrow(com_turnout_country)-1)

print(xtable(turnout_country_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Turnout country results of mixed effects moderator analysis \\label{tab:turnoutcountry}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(5), command = com_turnout_country ), sanitize.text.function = function(x) {x}, 
      file = "turnout-country-latex.tex")

#vote
vote_country_tab <- table.rma(vote_mixeff_country)
rownames(vote_country_tab) <- gsub("country_coarse", "", rownames(vote_country_tab))
vote_country_tab <- round(vote_country_tab,2)
vote_country_tab$estimate <- ifelse(between(vote_country_tab$p.value, 0, 0.001), paste0(vote_country_tab$estimate,"^{***}"), 
                                    ifelse(between(vote_country_tab$p.value, 0.001, 0.01), paste0(vote_country_tab$estimate,"^{**}"), 
                                           ifelse(between(vote_country_tab$p.value, 0.01, 0.05), paste0(vote_country_tab$estimate,"^{*}"),  paste0(vote_country_tab$estimate))))
vote_country_tab$p.value <- ifelse(vote_country_tab$p.value < 0.001, "<0.001", vote_country_tab$p.value)
vote_country_tab$k <- as.numeric(table(vaa_vote$country_coarse))

vote_country_latex<- latex_reg(vote_country_tab)

colnames(vote_country_latex) <- mixeff_cols
rownames(vote_country_latex) <- rownames(vote_country_tab)

com_vote_country  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                            "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 16) = 27.5137, p-val = 0.0361, QM(df = 5) = 19.6308, p-val = 0.0015  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_country)-1)

print(xtable(vote_country_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Vote choice country results of mixed effects moderator analysis \\label{tab:votecountry}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(5), command = com_vote_country ), sanitize.text.function = function(x) {x}, 
      file = "vote-country-latex.tex")


## Tables B5 and B6. election year effects --------------------
#turnout
turnout_election_year_tab <- table.rma(turnout_mixeff_election_year)
rownames(turnout_election_year_tab) <- gsub("election_year_ord", "", rownames(turnout_election_year_tab))
turnout_election_year_tab <- round(turnout_election_year_tab,2)
turnout_election_year_tab$estimate <- ifelse(between(turnout_election_year_tab$p.value, 0, 0.001), paste0(turnout_election_year_tab$estimate,"^{***}"), 
                                             ifelse(between(turnout_election_year_tab$p.value, 0.001, 0.01), paste0(turnout_election_year_tab$estimate,"^{**}"), 
                                                    ifelse(between(turnout_election_year_tab$p.value, 0.01, 0.05), paste0(turnout_election_year_tab$estimate,"^{*}"), turnout_election_year_tab$estimate)))
turnout_election_year_tab$p.value <- ifelse(turnout_election_year_tab$p.value < 0.001, "<0.001", turnout_election_year_tab$p.value)
turnout_election_year_tab$k <- as.numeric(table(vaa_turnout$election_year_ord))

turnout_election_year_latex<- latex_reg(turnout_election_year_tab)

colnames(turnout_election_year_latex) <- mixeff_cols
rownames(turnout_election_year_latex) <- rownames(turnout_election_year_tab)

com_turnout_election_year  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                     "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 25) = 127.1568, p-val < .0001, QM(df = 5) = 41.8469, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_turnout_election_year)-1)

print(xtable(turnout_election_year_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Turnout election year results of mixed effects moderator analysis \\label{tab:turnoutelectionyear}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(5), command = com_turnout_election_year ), sanitize.text.function = function(x) {x}, 
      file = "turnout-election-year-latex.tex")

#vote
vote_election_year_tab <- table.rma(vote_mixeff_election_year)
rownames(vote_election_year_tab) <- gsub("election_year_ord", "", rownames(vote_election_year_tab))
vote_election_year_tab <- round(vote_election_year_tab,2)
vote_election_year_tab$estimate <- ifelse(between(vote_election_year_tab$p.value, 0, 0.001), paste0(vote_election_year_tab$estimate,"^{***}"), 
                                          ifelse(between(vote_election_year_tab$p.value, 0.001, 0.01), paste0(vote_election_year_tab$estimate,"^{**}"), 
                                                 ifelse(between(vote_election_year_tab$p.value, 0.01, 0.05), paste0(vote_election_year_tab$estimate,"^{*}"),  paste0(vote_election_year_tab$estimate))))
vote_election_year_tab$p.value <- ifelse(vote_election_year_tab$p.value < 0.001, "<0.001", vote_election_year_tab$p.value)
vote_election_year_tab$k <- as.numeric(table(vaa_vote$election_year_ord))

vote_election_year_latex<- latex_reg(vote_election_year_tab)

colnames(vote_election_year_latex) <- mixeff_cols
rownames(vote_election_year_latex) <- rownames(vote_election_year_tab)

com_vote_election_year  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                  "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity is significant at the 95\\%: QE(df = 16) = 44.0260, p-val = 0.0002. The test of moderators is not significant: QM(df = 5) = 11.0442, p-val = 0.0505  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_election_year)-1)

print(xtable(vote_election_year_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Vote choice election year results of mixed effects moderator analysis \\label{tab:voteelectionyear}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(5), command = com_vote_election_year ), sanitize.text.function = function(x) {x}, 
      file = "vote-election-year-latex.tex")



## Tables B7 and B8. election type effects --------------------
#turnout
turnout_election_type_tab <- table.rma(turnout_mixeff_election_type)
rownames(turnout_election_type_tab) <- gsub("election_type_cat", "", rownames(turnout_election_type_tab))
turnout_election_type_tab <- round(turnout_election_type_tab,2)
turnout_election_type_tab$estimate <- ifelse(between(turnout_election_type_tab$p.value, 0, 0.001), paste0(turnout_election_type_tab$estimate,"^{***}"), 
                                             ifelse(between(turnout_election_type_tab$p.value, 0.001, 0.01), paste0(turnout_election_type_tab$estimate,"^{**}"), 
                                                    ifelse(between(turnout_election_type_tab$p.value, 0.01, 0.05), paste0(turnout_election_type_tab$estimate,"^{*}"), turnout_election_type_tab$estimate)))
turnout_election_type_tab$p.value <- ifelse(turnout_election_type_tab$p.value < 0.001, "<0.001", turnout_election_type_tab$p.value)
turnout_election_type_tab$k <- as.numeric(table(vaa_turnout$election_type_cat))

turnout_election_type_latex<- latex_reg(turnout_election_type_tab)

colnames(turnout_election_type_latex) <- mixeff_cols
rownames(turnout_election_type_latex) <- rownames(turnout_election_type_tab)

com_turnout_election_type  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                     "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 28) = 131.7178, p-val < .0001, QM(df = 2) = 32.4878, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_turnout_election_type)-1)

print(xtable(turnout_election_type_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Turnout election type results of mixed effects moderator analysis \\label{tab:turnoutelectiontype}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(2), command = com_turnout_election_type ), sanitize.text.function = function(x) {x}, 
      file = "turnout-election-type-latex.tex")

#vote
vote_election_type_tab <- table.rma(vote_mixeff_election_type)
rownames(vote_election_type_tab) <- gsub("election_type_cat", "", rownames(vote_election_type_tab))
vote_election_type_tab <- round(vote_election_type_tab,2)
vote_election_type_tab$estimate <- ifelse(between(vote_election_type_tab$p.value, 0, 0.001), paste0(vote_election_type_tab$estimate,"^{***}"), 
                                          ifelse(between(vote_election_type_tab$p.value, 0.001, 0.01), paste0(vote_election_type_tab$estimate,"^{**}"), 
                                                 ifelse(between(vote_election_type_tab$p.value, 0.01, 0.05), paste0(vote_election_type_tab$estimate,"^{*}"),  paste0(vote_election_type_tab$estimate))))
vote_election_type_tab$p.value <- ifelse(vote_election_type_tab$p.value < 0.001, "<0.001", vote_election_type_tab$p.value)
vote_election_type_tab$k <- as.numeric(table(vaa_vote$election_type_cat))

vote_election_type_latex<- latex_reg(vote_election_type_tab)

colnames(vote_election_type_latex) <- mixeff_cols
rownames(vote_election_type_latex) <- rownames(vote_election_type_tab)

com_vote_election_type  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                  "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 19) = 45.5668, p-val = 0.0006, QM(df = 2) = 10.7327, p-val = 0.0047  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_election_type)-1)

print(xtable(vote_election_type_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Vote choice election type results of mixed effects moderator analysis \\label{tab:voteelectiontype}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(2), command = com_vote_election_type ), sanitize.text.function = function(x) {x}, 
      file = "vote-election-type-latex.tex")


## Tables B9 and B10. sample size -------------------------
#turnout
turnout_sample_tab <- table.rma(turnout_mixeff_sample)
rownames(turnout_sample_tab) <- gsub("sample_size_ord", "", rownames(turnout_sample_tab))
turnout_sample_tab <- round(turnout_sample_tab,2)
turnout_sample_tab$estimate <- ifelse(between(turnout_sample_tab$p.value, 0, 0.001), paste0(turnout_sample_tab$estimate,"^{***}"), 
                                      ifelse(between(turnout_sample_tab$p.value, 0.001, 0.01), paste0(turnout_sample_tab$estimate,"^{**}"), 
                                             ifelse(between(turnout_sample_tab$p.value, 0.01, 0.05), paste0(turnout_sample_tab$estimate,"^{*}"), turnout_sample_tab$estimate)))
turnout_sample_tab$p.value <- ifelse(turnout_sample_tab$p.value < 0.001, "<0.001", turnout_sample_tab$p.value)

turnout_sample_tab$k <- as.numeric(table(vaa_turnout$sample_size_ord))

turnout_sample_latex<- latex_reg(turnout_sample_tab)

colnames(turnout_sample_latex) <- mixeff_cols
rownames(turnout_sample_latex) <- rownames(turnout_sample_tab)

com_turnout_sample  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                              "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 27) = 133.6281, p-val < .0001, QM(df = 3) = 48.1064, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_turnout_sample)-1)

print(xtable(turnout_sample_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Turnout sample size results of mixed effects moderator analysis \\label{tab:turnoutsample}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(3), command = com_turnout_sample ), sanitize.text.function = function(x) {x}, 
      file = "turnout-sample-size-latex.tex")

#vote
vote_sample_tab <- table.rma(vote_mixeff_sample)
rownames(vote_sample_tab) <- gsub("sample_size_ord", "", rownames(vote_sample_tab))
vote_sample_tab <- round(vote_sample_tab,2)
vote_sample_tab$estimate <- ifelse(between(vote_sample_tab$p.value, 0, 0.001), paste0(vote_sample_tab$estimate,"^{***}"), 
                                   ifelse(between(vote_sample_tab$p.value, 0.001, 0.01), paste0(vote_sample_tab$estimate,"^{**}"), 
                                          ifelse(between(vote_sample_tab$p.value, 0.01, 0.05), paste0(vote_sample_tab$estimate,"^{*}"),  paste0(vote_sample_tab$estimate))))
vote_sample_tab$p.value <- ifelse(vote_sample_tab$p.value < 0.001, "<0.001", vote_sample_tab$p.value)
vote_sample_tab$k <- as.numeric(table(vaa_vote$sample_size_ord))

vote_sample_latex<- latex_reg(vote_sample_tab)

colnames(vote_sample_latex) <- mixeff_cols
rownames(vote_sample_latex) <- rownames(vote_sample_tab)

com_vote_sample  <-  paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                            "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The test for residual heterogeneity is not significant at the 95\\%: QE(df = 18) = 26.6059, p-val = 0.0867. The test of moderators is significant:  QM(df = 3) = 38.3041, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_sample)-1)

print(xtable(vote_sample_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Vote choice sample size results of mixed effects moderator analysis \\label{tab:votesample}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(3), command = com_vote_sample ), sanitize.text.function = function(x) {x}, 
      file = "vote-sample-size-latex.tex")


## Tables B11 and B12. sampling_type -------------------------

#turnout
turnout_sampling_type_tab <- table.rma(turnout_mixeff_sampling_type)
rownames(turnout_sampling_type_tab) <- gsub("sampling_type", "", rownames(turnout_sampling_type_tab))
turnout_sampling_type_tab <- round(turnout_sampling_type_tab,2)
turnout_sampling_type_tab$estimate <- ifelse(between(turnout_sampling_type_tab$p.value, 0, 0.001), paste0(turnout_sampling_type_tab$estimate,"^{***}"), 
                                             ifelse(between(turnout_sampling_type_tab$p.value, 0.001, 0.01), paste0(turnout_sampling_type_tab$estimate,"^{**}"), 
                                                    ifelse(between(turnout_sampling_type_tab$p.value, 0.01, 0.05), paste0(turnout_sampling_type_tab$estimate,"^{*}"), turnout_sampling_type_tab$estimate)))
turnout_sampling_type_tab$p.value <- ifelse(turnout_sampling_type_tab$p.value < 0.001, "<0.001", turnout_sampling_type_tab$p.value)

turnout_sampling_type_tab$k <- as.numeric(table(vaa_turnout$sampling_type))

turnout_sampling_type_latex<- latex_reg(turnout_sampling_type_tab)

colnames(turnout_sampling_type_latex) <- mixeff_cols
rownames(turnout_sampling_type_latex) <- rownames(turnout_sampling_type_tab)

com_turnout_sampling_type  <- paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                     "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 25) = 133.5, p-val < .0001, QM(df = 2) = 27.99, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_turnout_sampling_type)-1)

print(xtable(turnout_sampling_type_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Turnout sampling type results of mixed effects moderator analysis \\label{tab:turnoutsampling_type}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(2), command = com_turnout_sampling_type), sanitize.text.function = function(x) {x}, 
      file = "turnout-sampling_type-latex.tex")

#vote
vote_sampling_type_tab <- table.rma(vote_mixeff_sampling_type)
rownames(vote_sampling_type_tab) <- gsub("sampling_type", "", rownames(vote_sampling_type_tab))
vote_sampling_type_tab <- round(vote_sampling_type_tab,2)
vote_sampling_type_tab$estimate <- ifelse(between(vote_sampling_type_tab$p.value, 0, 0.001), paste0(vote_sampling_type_tab$estimate,"^{***}"), 
                                          ifelse(between(vote_sampling_type_tab$p.value, 0.001, 0.01), paste0(vote_sampling_type_tab$estimate,"^{**}"), 
                                                 ifelse(between(vote_sampling_type_tab$p.value, 0.01, 0.05), paste0(vote_sampling_type_tab$estimate,"^{*}"),  paste0(vote_sampling_type_tab$estimate))))
vote_sampling_type_tab$p.value <- ifelse(vote_sampling_type_tab$p.value < 0.001, "<0.001", vote_sampling_type_tab$p.value)
vote_sampling_type_tab$k <- as.numeric(table(vaa_vote$sampling_type))

vote_sampling_type_latex<- latex_reg(vote_sampling_type_tab)

colnames(vote_sampling_type_latex) <- mixeff_cols
rownames(vote_sampling_type_latex) <- rownames(vote_sampling_type_tab)

com_vote_sampling_type  <-  paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                   "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 18) = 34.62, p-val = 0.011, QM(df = 3) = 21.14, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_sampling_type)-1)

print(xtable(vote_sampling_type_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Vote choice sample type results of mixed effects moderator analysis \\label{tab:votesampling_type}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(3), command = com_vote_sampling_type ), sanitize.text.function = function(x) {x}, 
      file = "vote-sampling_type-latex.tex")


## Table B13. vote choice operationalization -------------------------

#vote
vote_vote_change_tab <- table.rma(vote_mixeff_vote_change)
rownames(vote_vote_change_tab) <- gsub("depvar_cat", "", rownames(vote_vote_change_tab))
vote_vote_change_tab <- round(vote_vote_change_tab,2)
vote_vote_change_tab$estimate <- ifelse(between(vote_vote_change_tab$p.value, 0, 0.001), paste0(vote_vote_change_tab$estimate,"^{***}"), 
                                        ifelse(between(vote_vote_change_tab$p.value, 0.001, 0.01), paste0(vote_vote_change_tab$estimate,"^{**}"), 
                                               ifelse(between(vote_vote_change_tab$p.value, 0.01, 0.05), paste0(vote_vote_change_tab$estimate,"^{*}"),  paste0(vote_vote_change_tab$estimate))))
vote_vote_change_tab$p.value <- ifelse(vote_vote_change_tab$p.value < 0.001, "<0.001", vote_vote_change_tab$p.value)
vote_vote_change_tab$k <- as.numeric(table(vaa_vote$depvar_cat))

vote_vote_change_latex <- latex_reg(vote_vote_change_tab)

colnames(vote_vote_change_latex) <- mixeff_cols
rownames(vote_vote_change_latex) <- rownames(vote_vote_change_tab)

com_vote_vote_change  <-  paste0("\\bottomrule\n \\multicolumn{8}{p{13cm}}",
                                 "{\\scriptsize{\\textbf{Notes:} The estimates are presented in log-odds. The tests for residual heterogeneity and of moderators are significant at the 95\\%: QE(df = 19) = 34.43, p-val = 0.016, QM(df = 3) = 19.45, p-val < .0001  }} \\\\\n")

hlines <- c(-1, 0, nrow(com_vote_vote_change)-1)

print(xtable(vote_vote_change_latex, align = c("r", "l", "c", "c", "c", "c", "c", "c"), 
             digits=2, caption = "Operationalization of vote choice results of mixed effects moderator analysis \\label{tab:vote-vote_change}"), 
      booktabs = TRUE, hline.after = hlines, size = "footnotesize", caption.placement = "top", table.placement = "t!bh", 
      include.rownames=TRUE, add.to.row = list(pos = list(2), command = com_vote_vote_change ), sanitize.text.function = function(x) {x}, 
      file = "vote-vote_change-latex.tex")


